﻿using Zbk_shop.Models;
using Zbk_shop.Models.Database;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Zbk_shop.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodController : ControllerBase
    {
        private readonly Zbk_shopContext _db;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public GoodController(Zbk_shopContext db)
        {
            _db = db;
        }

        /// <summary>
        /// 新增商品 
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddGood(Good good)
        {
            _db.Goods.Add(good);
            var row = _db.SaveChanges();
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }
        /// <summary>
        /// 修改商品 
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateGood(Good good)
        {
            var model = _db.Goods.FirstOrDefault(x => x.GoodId == good.GoodId);
            if (model == null)
                return "商品不存在";

            model.Photo = good.Photo;
            model.Title = good.Title;
            model.Desc = good.Desc;
            model.State = good.State;
            model.Stock = good.Stock;
            model.CateId = good.CateId;

            _db.Goods.Update(model);
            var row = _db.SaveChanges();
            if (row > 0)
                return "编辑成功";
            return "编辑失败";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<Good> GetGoodList()
        {
            return _db.Goods.ToList();
        }
        /// <summary>
        /// 刪除商品
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        [HttpPost]
        public string DeleteGood(int goodId)
        {
            var model = _db.Goods.FirstOrDefault(x => x.GoodId == goodId);
            if (model == null)
                return "商品不存在";


            _db.Goods.Remove(model);
            var row = _db.SaveChanges();
            if (row > 0)
                return "刪除成功";
            return "刪除失败";
        }
        /// <summary>
        /// 新增分类
        /// </summary>
        /// <param name="goodCategory"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddGoodCategory(GoodCategory goodCategory)
        {
            _db.GoodCategories.Add(goodCategory);
            var row = _db.SaveChanges();
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }

        /// <summary>
        /// 修改分类
        /// </summary>
        /// <param name="goodCategory"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateGoodCategory(GoodCategory goodCategory)
        {
            var model = _db.GoodCategories.FirstOrDefault(x => x.CateId == goodCategory.CateId);
            if (model == null)
                return "分类不存在";
            model.CateName = goodCategory.CateName;
            _db.GoodCategories.Update(model);
            var row = _db.SaveChanges();
            if (row > 0)
                return "修改成功";
            return "修改失败";
        }
        /// <summary>
        /// 分类列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<GoodCategory> GetGoodCategories()
        {
            return _db.GoodCategories.ToList();
        }
        /// <summary>
        /// 刪除分类
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string DeleteGoodCategory(int cateId)
        {
            var model = _db.GoodCategories.FirstOrDefault(x => x.CateId == cateId);
            if (model == null)
                return "分类不存在";
            _db.GoodCategories.Remove(model);
            var row = _db.SaveChanges();
            if (row > 0)
                return "刪除成功";
            return "刪除失败";
        }

        //前端商品列表展示（按照商品分类、商品名称搜索展示商品列表，可分页展示）
        /// <summary>
        /// 前端商品列表展示
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public GetGoodShowResponse GetGoodShow(GetGoodShowRequest request)
        {
            //固定
            var good = _db.Goods.Where(x => x.CateId == request.CateId);
            //如果传入关键字不为空 查询
            if (!string.IsNullOrEmpty(request.Keyword))
            {
                //模糊查询
                good = good.Where(x => x.Title.Contains(request.Keyword));
            }

            var total = good.Count();
            //分页
            var list = good
                 //排序 分页前一定要排序
                 .OrderByDescending(x => x.GoodId)
                 //跳过n===((pageIndex-1)*pageSize) 条 SQL SERVER 2012以上
                 .Skip((request.PageIndex - 1) * request.PageSize)
                 //取x条数据
                 .Take(request.PageSize)
                 //把数据库商品对象转换成自定义类
                 .Select(x => new GoodShowModel
                 {
                     GoodId = x.GoodId,
                     Photo = x.Photo,
                     Title = x.Title
                 })
                 .ToList();
            //构造返回
            var response = new GetGoodShowResponse
            {
                List = list,
                //总数 给前端 判断共多少页 total/pagesize
                Total = total
            };
            return response;
        }

        /// <summary>
        /// 获取商品查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public Good GetGoodInfo(int goodId)
        {
            var good = _db.Goods.FirstOrDefault(x => x.GoodId == goodId);
            return good;
        }

    }
}
